﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NSDecisionTree.Tools;

namespace NSDecisionTree.Classes
{
    class DecisionTable
    {
        #region private

        int currentIndex;
        string resultCaption;

        List<string> captions;
        List<KeyValuePair<int, List<string>>> rowContent;
        List<string> decisions;

        #endregion

        #region public

        public int Count
        {
            get { return ( currentIndex - 1); }
        }

        public List<string> Captions
        {
            get { return captions; }
        }
        
        public List<KeyValuePair<int, List<string>>> RowContent
        {
          get { return rowContent; }
        }

        public string ResultCaption
        {
            get { return resultCaption; }
        }

        #endregion

        #region ctor

        public DecisionTable(List<string> ObjectCaptions)
        {
            currentIndex = 1;
            captions = ObjectCaptions;
            rowContent = new List<KeyValuePair<int, List<string>>>();
            decisions = new List<string>();
            resultCaption = ObjectCaptions.ElementAt(ObjectCaptions.Count - 1);
        }

        #endregion

        #region Methods

        public void AddRowItem(List<string> ObjectRowItem)
        {
            KeyValuePair<int, List<string>> kv = new KeyValuePair<int, List<string>>(currentIndex,ObjectRowItem);
            rowContent.Add(kv);
            currentIndex++;
        }

        public void AddResultItems(List<string> Decisions)
        {
            Decisions.Sort();
            decisions = Decisions;
        }

        public string[] GetAsCsvStringArray(string delimiter)
        {
            string[] retVal = new string[currentIndex];
            retVal[0] = captions.ToConcatedString(delimiter);
            int i = 1;
            foreach (KeyValuePair<int, List<string>> element in rowContent.ToArray())
            {
                retVal[i] = element.Value.ToConcatedString(delimiter);
                i++;
            }

            return retVal;
        }

        #endregion
    }
}
